<?php

namespace app\health\service;

use think\Db;
use think\Log;

class AdsApi
{
    //巨量纵横回传api 事件、点击id,手机号,pid,落地页地址,
    public static function JlApi($event_type,$clickid,$mobile,$pid,$curl,$bus_code=''){
        $data = [
            'event_type'=>$event_type,//form
            'context' => [
                'ad'=>[
                    'callback'=>$clickid,
                ],
            ],
            'timestamp' =>time(),
        ];
        $adRes = self::CurlPost('https://analytics.oceanengine.com/api/v2/conversion',$data,true,'JuLiang');
        $adArr = json_decode($adRes,true);
        $ad = [
            'mobile'=>$mobile,
            'event_type'=>'form',
            'clickid'=>$clickid,
            'bus_code'=>$bus_code,
            'pid'=>$pid,
            'url'=>$curl,
            'add_time'=>time(),
            'code'=>$adArr['code'],
            'message'=>$adArr['message'],
            'source'=>'jl',
        ];
        Db::connect('cladmin')->name('tui_ad')->insertGetId($ad);
    }

    //腾讯web广告回传接口
    public static function TxApi($action_type,$clickid,$mobile,$pid,$curl,$bus_code=''){
        $host = parse_url($curl);
        $data = [
            'actions'=>[
                [
                    'action_time'=>time(),
                    'action_type'=>$action_type,
                    'url'=> $host['host'] ?? '',
                    'trace'=>[
                        'click_id'=>$clickid,
                    ],
                ],
            ],
        ];
        $adRes = self::CurlPost('http://tracking.e.qq.com/conv',$data,true,'tx');
        $adArr = json_decode($adRes,true);
        $ad = [
            'mobile'=>$mobile,
            'event_type'=>$action_type,
            'clickid'=>$clickid,
            'bus_code'=>$bus_code,
            'pid'=>$pid,
            'url'=>$curl,
            'add_time'=>time(),
            'code'=>$adArr['code'],
            'message'=>$adArr['message'],
            'source'=>'tx',
        ];
        Db::connect('cladmin')->name('tui_ad')->insertGetId($ad);
    }

    //腾讯小程序广告回传接口
    public static function TxMiniApi($action_type,$clickid,$mobile,$pid,$curl,$bus_code=''){
        $clickid_arr = json_decode(htmlspecialchars_decode($clickid),true);
        $wechat_openid  = isset($clickid_arr['openid'])?$clickid_arr['openid']:'';
        $wechat_app_id = isset($clickid_arr['appid'])?$clickid_arr['appid']:'';
        $clickid = isset($clickid_arr['clickid'])?$clickid_arr['clickid']:'';
        $data = [
            'actions'=>[
                [
                    'action_time'=>time(),
                    'user_id'=>[
                        'wechat_openid' =>$wechat_openid,
                        'wechat_app_id' =>$wechat_app_id,
                    ],
                    'action_type'=>$action_type,
                    'trace'=>[
                        'click_id'=>$clickid,
                    ],
                ],
            ],
        ];
        $adRes = self::CurlPost('http://tracking.e.qq.com/conv',$data,true,'txmini');
        $adArr = json_decode($adRes,true);
        $ad = [
            'mobile'=>$mobile,
            'event_type'=>$action_type,
            'clickid'=>$clickid,
            'bus_code'=>$bus_code,
            'pid'=>$pid,
            'url'=>$curl,
            'add_time'=>time(),
            'code'=>$adArr['code'],
            'message'=>$adArr['message'],
            'source'=>'txmini',
        ];
        Db::connect('cladmin')->name('tui_ad')->insertGetId($ad);
    }

    /**
     */
    private static function CurlPost($url, $post = [], $is_json = false,$source='')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        //连接超时时间
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        //超时时间
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        // 是否 json
        if($is_json){
            $data_string = json_encode($post);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                    "Content-Type: application/json; charset=utf-8",
                    "Content-Length: " . strlen($data_string)
                )
            );
        } else {
            if(!empty($post)){
                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
            }
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                    "Content-Type: application/x-www-form-urlencoded",
                    "cache-control: no-cache"
                )
            );
        }
        $curl_errno = curl_errno($ch);
        $curl_error = curl_error($ch);
        $result = curl_exec($ch);
        curl_close($ch);
        Log::debug(date("Y-m-d H:i:s").$source.' '.$url.json_encode($post).'结果：'.$result);
        return $result;
    }
}